<html>
    <head>
        <title>GoogleReaderPlus Icon Background</title>
		<script type="application/javascript" src="lib/jquery-1.4.2.min.js"></script>
		<script type="application/javascript" src="lib/webkit.js"></script>
        <script type="application/javascript" src="lib/xml2json.js"></script>
		<script type="application/javascript" src="config.js"></script>
        <script type="application/javascript">
			//UTF8: é
			//Debug configuration
			setPrefs({
				badge: {
					id:6,//A3_france
					interval: 10
				}
			});
			var prefs=getPrefs(),badgeprefs=getPref('badge');
			
			//var notifier = new Notifier();
			
			updateTimes();
			var interval=(badgeprefs && badgeprefs.interval)?badgeprefs.interval:60;
			window.setInterval(updateTimes, interval*1000);
            
            chrome.extension.onRequest.addListener(function(a, sender, callback){
                if (a.message === 'xhr') {
                    request(a, false, callback);
                } else if (a.message === 'badge') {
                    updateBadge(a, callback);
                } else if (a.message === 'updatetimes') {
                    updateTimes(a, callback);
                } else if (a.message === 'updatealerts') {
                    updateAlerts(a, callback);
                } else if (a.message === 'updateflashs') {
                    updateFlashs(a, callback);
                }
            });
			
            function setPrefs(prefs, cleanall){
                if (cleanall){
					localStorage.clear();
				}
				localStorage.setItem('prefs', JSON.stringify(prefs));
            }
			
           function getPrefs(){
                var id;
                var textPrefs = localStorage.getItem('prefs');
                var prefs = (textPrefs) ? JSON.parse(textPrefs) : false;
                //prefs = checkPrefs(prefs);
                return prefs;
            }
			
            function getPref(option){
                if (prefs && typeof prefs[option] !== "undefined") {
                    return prefs[option];
                } else {
                    return false;
                }
            }
            
            function updateBadge(a, cb){				
				//Send time to badge
                var time = parseInt(a.time, 10);

                var color = 'Green';
                var bcolor = [0, 255, 0, 255];
                if (time > 20) {
                    color = 'Red';
                    bcolor = [255, 0, 0, 255];
                } else if (time > 10) {
                    color = 'Orange';
                    bcolor = [255, 143, 53, 255];
                }
                chrome.browserAction.setBadgeText({
                    text: '' + time
                });
                chrome.browserAction.setBadgeBackgroundColor({
                    color: bcolor
                });
				var icon = 'images/16/Box_' + color + '.png';
                chrome.browserAction.setIcon({
                    path: icon
                });
				
				var title= ''+timeTexts[a.id] + '\n' + time + ' minutes';
                chrome.browserAction.setTitle({
                    title: title
                });
				
				//var geo = new Geolocation();
				//geo.getCurrentPosition();
				/*if (!notifier.notify(icon, title, 'Time is ' + time + ' minutes')){
					notifier.requestPermission(function(status){
						console.log('statusPermission='+status);
					});
				}
				*/
				
                if (cb) {
                    cb(null);
                }
            }
			
            //Temps de parcours etat.lu
            function updateTimes(a, callback){
				xhr({
                    method: 'GET',
                    url: urlTime,
                    dataType: 'text'
                }, function(xhr){
                    var m, times = {}, data = xhr.responseText;
                    //&1A6_lux_sud=LUX-SUD  24 min.
                    while ((m = reTime.exec(data)) !== null) {
                        var id = m[1], code = m[2], zone = m[3], time = m[4];
						if (time==='FLUIDE'){
							time=0;
						}else{
							var n = /\d+/.exec(time);
							if (n){
								time=parseInt(n[0],10);
							}else{
								time=0;
							}
						}
						
                        if (!times[zone]) {
                            times[zone] = [];
                        }
                        times[zone].push({id:id,code:code,time:time});
                        if (badgeprefs && (''+badgeprefs.id) === id ) {
                            updateBadge({
                                id:id,
								time: time,
								code:code
                            });
                        }
                    }
                    if (callback) {
                        callback(times);
                    }
                });
            }
            
            //alert info cita.lu
            function updateAlerts(a, callback){
                xhr({
                    method: 'GET',
                    url: urlAlert,
                    dataType: 'xml'
                }, function(xhr){
                    var o = JSON.parse(xhr.responseJson);
                    var channel = o.rss.channel;
                    if (callback) {
                        callback(channel);
                    }
                });
            }
            
            //flashinfo lesfrontaliers.lu
            function updateFlashs(a, callback){
                xhr({
                    method: 'GET',
                    url: urlFlashInfo,
                    dataType: 'text'
                }, function(xhr){
                    var news, m, data = xhr.responseText;
                    m = reFlashInfoLF.main.exec(data);
                    if (m && m[1]) {
                        var n = reFlashInfoLF.title.exec(m[1]), p = reFlashInfoLF.content.exec(m[1]);
                        var title = n[1], content = p[1];
                        news = {
                            title: title,
                            content: content
                        };
                        if (callback) {
                            callback(news);
                        }
                    }
                });
            }
            
            function xhr(a, cb){
                request(a, true, cb);
            }
            
            function request(a, local, cb){
                var xhr = new XMLHttpRequest();
                var method = a.method || 'get';
                var url = a.url;
                if (a.parameters && (method.toLowerCase() == 'get')) {
                    var params = [];
                    for (var k in a.parameters) {
                        params.push(k + '=' + encodeURIComponent(a.parameters[k]));
                    }
                    var sep = (url.indexOf('?') > 0) ? '&' : '?';
                    url += sep + params.join('&');
                }
                xhr.open(a.method || 'get', url, true);
                //headers
                if (a.headers) {
                    for (var kh in a.headers) {
                        xhr.setRequestHeader(kh, a.headers[kh]);
                    }
                }
                if (cb) {
                    a.onreadystatechange = true;//Chrome extesnion looks on readystatechange
                }
                xhr.onreadystatechange = function(o){
                    if (xhr.readyState == 4) {
                        var responseJson = false;
                        if (a.dataType === 'xml' && xhr.responseXML) {
                            responseJson = xml2json(xhr.responseXML, '');
                        }
                        cb({
                            responseJson: responseJson,
                            responseText: xhr.responseText,
                            responseHeaders: xhr.responseHeaders,
                            status: xhr.status,
                            statusText: xhr.statusText,
                            request: a
                        });
                    }
                };
                try {
                    xhr.send(a.data || {});
                } catch (e) {
                    var o = {
                        responseText: e.message || 'Error',
                        status: (e.name || '') + ' ' + (e.code || 0),
                        statusText: (e.name || '') + ' ' + (e.code || 0),
                        error: e
                    };
                    if (local && typeof a.onerror === "function") {
                        a.onerror.call(this, o);
                    } else {
                        cb(o);
                    }
                }
            }
        </script>
</head>
<body>
	<!--canvas id="canvas" width="19" height="19"></canvas-->
</body>
</html>
